package com.example.diary

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_detail.*
import kotlinx.android.synthetic.main.activity_edit.btn_quit
import kotlinx.android.synthetic.main.array_adapter.*

class Detail : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_detail)
        val dbHelper =MyDatabaseHelper(this,"diary.db",1)
        val db=dbHelper.writableDatabase

        val bundle=this.intent.extras
        val title = bundle?.get("标题").toString()
        edit_title.setText(title)

        val cursor = db.query("DiaryData",
            null, "title=?", arrayOf(title),null, null, null )
        if (cursor.moveToFirst()) {
            do {
                // 遍历Cursor对象，取出数据并打印
                val t = cursor.getString(cursor.getColumnIndex("title"))
                val c = cursor.getString(cursor.getColumnIndex("content"))
                val ti = cursor.getString(cursor.getColumnIndex("time"))
                Log.d("Detail", "title is $t")
                Log.d("Detail", "content is $c")
                Log.d("Detail", "time is $time")
                edit_content.setText(c)
                time.setText(ti)

            } while (cursor.moveToNext())
        }
        cursor.close()


        btn_quit.setOnClickListener {
            Toast.makeText(this, "返回首页！", Toast.LENGTH_SHORT).show()
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
        }
        btn_delete.setOnClickListener {
            Toast.makeText(this, "删除成功！", Toast.LENGTH_SHORT).show()
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
        }
    }
}